package com.edgclub.edg.activity;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;

import java.util.Timer;
import java.util.TimerTask;

public class SettingChangePhoneActivity extends GzBaseTooolBarActivity implements View.OnClickListener, VolleyListener {

    private TextView mPhone_num;
    private TextView mText_content;
    private RelativeLayout mMess_code_layout;
    private EditText mMess_code;
    private Button mGet_code_btn;
    private Button mSubmit;
    private int time = 120;
    private Timer timer;
    private TimerTask timerTask;
    private UserPrefUtils userPrefUtils;
    private String getpsw = "2";


    @Override
    public int getContentView() {
        return R.layout.activity_change_phone;
    }

    @Override
    public void setTitleBarView() {
        userPrefUtils = new UserPrefUtils(this);
        setCustomActionBar();
        title.setText("更换手机号码");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }

    @Override
    public void initView() {
        mPhone_num = (TextView) findViewById(R.id.phone_num);
        mText_content = (TextView) findViewById(R.id.text_content);
        mMess_code_layout = (RelativeLayout) findViewById(R.id.mess_code_layout);
        mMess_code = (EditText) findViewById(R.id.mess_code);
        mGet_code_btn = (Button) findViewById(R.id.get_code_btn);
        mSubmit = (Button) findViewById(R.id.submit);
        mGet_code_btn.setOnClickListener(this);
        mSubmit.setOnClickListener(this);

        String zh=userPrefUtils.getUser().getPhone()+"";
        String zhTmp="";
        zhTmp=zh.substring(0,5)+"****"+zh.substring(9,zh.length());
        mPhone_num.setTextColor(getResources().getColor(R.color.c3));
        mPhone_num.setText(zhTmp);

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_back:
                finish();
                break;
            case R.id.get_code_btn:
                getCode();
                break;
            case R.id.submit:
                if ("更换手机号码".equals(mSubmit.getText().toString())) {
                    mMess_code_layout.setVisibility(View.VISIBLE);
                    mSubmit.setText("确定");
                    mText_content.setText("为了您的账号安全，需先验证已绑定的手机，点击“获取验证码”进行验证");

                    break;
                }else{
                    oksubmit();
                    break;
                }

        }
    }

    private void getCode() {
        UserParam param = new UserParam();
        param.setUid(userPrefUtils.getUser().getPhone());
        param.setTarget("updatePhone");
        new UserApi(this).registerGetcode(param, this);

    }

    public void oksubmit() {
        if (TextUtils.isEmpty(mMess_code.getText().toString().trim())) {
            Toast.makeText(SettingChangePhoneActivity.this,"请输验证码",Toast.LENGTH_SHORT).show();
            return;
        }
        UserParam param = new UserParam();
            param.setUid(userPrefUtils.getUser().getPhone()+"");
            param.setTarget("updatePhone");
            param.setCode(mMess_code.getText().toString().trim());
        new UserApi(this).registerCode(param,this);


    }


    @Override
    public void onStart(String tag) {
        if (ApiConfig.APIURL.URL_REGISTER_CODE .equals(tag)) {
            showDialog(StringUtils.getString(R.string.get_identifying), false);
        }else if (ApiConfig.APIURL.URL_REGISTER_CODE1 .equals(tag)){
            showDialog(StringUtils.getString(R.string.get_login), false);
        }
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        if (ApiConfig.APIURL.URL_REGISTER_CODE .equals(tag)) {

            if (JSONHelper.isSuccess(response)) {
                Toast.makeText(SettingChangePhoneActivity.this, StringUtils.getString(R.string.checkverification),
                        Toast.LENGTH_LONG).show();
                mGet_code_btn.setEnabled(false);
                startTimer();
            }
            else if (JSONHelper.getResultCode(response) == 1003) {
                Toast.makeText(SettingChangePhoneActivity.this,
                        StringUtils.getString(R.string.verificationerror), Toast.LENGTH_LONG).show();

            }
            else {
                Toast.makeText(
                        SettingChangePhoneActivity.this,
                        StringUtils.getString(R.string.verificationfail) + ":code(" + JSONHelper.getResultCode(response)
                                + ")", Toast.LENGTH_LONG).show();
            }
        }else if (ApiConfig.APIURL.URL_REGISTER_CODE1 .equals(tag)){
            if (JSONHelper.isSuccess(response)) {
                Toast.makeText(this,"验证成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(SettingChangePhoneActivity.this,RegisterActivity.class);
                intent.putExtra("getpsw", getpsw);
                startActivity(intent);
                finish();
            }
            else if (JSONHelper.getResultCode(response) == 1003) {
                Toast.makeText(SettingChangePhoneActivity.this,
                        StringUtils.getString(R.string.verificationerror), Toast.LENGTH_LONG).show();

            }else {
                Toast.makeText(SettingChangePhoneActivity.this,
                        "验证失败，请重新输入", Toast.LENGTH_LONG).show();
                return;
            }
        }

    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        dismissDialog();
        ResultCode.toastVolleyError(SettingChangePhoneActivity.this, volleyError);
    }


    class CountDownTimerTask extends TimerTask {

        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    }

    private void startTimer() {
        mGet_code_btn.setBackgroundResource(R.drawable.register_selector);
        mGet_code_btn.setTextColor(getResources().getColor(R.color.c5));
        time = 60;
        if (null == timer) {
            timer = new Timer();
        }

        timerTask = new CountDownTimerTask();
        timer.schedule(timerTask, 1000, 1000);
    }

    private void stopTimer() {
        mGet_code_btn.setBackgroundResource(R.drawable.shape_gz_get_code_bg);
        mGet_code_btn.setTextColor(getResources().getColor(R.color.c10));
        if (null != timer) {
            timer.cancel();
            timer = null;
        }

        if (null != timerTask) {
            timerTask.cancel();
        }

    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    time--;
                    mGet_code_btn.setText(time + StringUtils.getString(R.string.sresend));
                    if (time == 0) {
                        stopTimer();
                        time = 60;
                        mGet_code_btn.setEnabled(true);
                        mGet_code_btn.setText(StringUtils.getString(R.string.sendverification));
                    }
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };
}
